{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np \n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.dates as mdates\n",
    "from matplotlib import style"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1990-12-19</th>\n",
       "      <td>NaN</td>\n",
       "      <td>96.050</td>\n",
       "      <td>99.980</td>\n",
       "      <td>95.790</td>\n",
       "      <td>99.980</td>\n",
       "      <td>126000.00</td>\n",
       "      <td>4.940000e+05</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-20</th>\n",
       "      <td>99.980</td>\n",
       "      <td>104.300</td>\n",
       "      <td>104.390</td>\n",
       "      <td>99.980</td>\n",
       "      <td>104.390</td>\n",
       "      <td>19700.00</td>\n",
       "      <td>8.400000e+04</td>\n",
       "      <td>0.044109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-21</th>\n",
       "      <td>104.390</td>\n",
       "      <td>109.070</td>\n",
       "      <td>109.130</td>\n",
       "      <td>103.730</td>\n",
       "      <td>109.130</td>\n",
       "      <td>2800.00</td>\n",
       "      <td>1.600000e+04</td>\n",
       "      <td>0.045407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-24</th>\n",
       "      <td>109.130</td>\n",
       "      <td>113.570</td>\n",
       "      <td>114.550</td>\n",
       "      <td>109.130</td>\n",
       "      <td>114.550</td>\n",
       "      <td>3200.00</td>\n",
       "      <td>3.100000e+04</td>\n",
       "      <td>0.049666</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-25</th>\n",
       "      <td>114.550</td>\n",
       "      <td>120.090</td>\n",
       "      <td>120.250</td>\n",
       "      <td>114.550</td>\n",
       "      <td>120.250</td>\n",
       "      <td>1500.00</td>\n",
       "      <td>6.000000e+03</td>\n",
       "      <td>0.049760</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-24</th>\n",
       "      <td>2748.918</td>\n",
       "      <td>2770.754</td>\n",
       "      <td>2863.152</td>\n",
       "      <td>2761.372</td>\n",
       "      <td>2863.126</td>\n",
       "      <td>4776195.45</td>\n",
       "      <td>4.427953e+07</td>\n",
       "      <td>0.041547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-25</th>\n",
       "      <td>2863.126</td>\n",
       "      <td>2901.419</td>\n",
       "      <td>2952.451</td>\n",
       "      <td>2889.048</td>\n",
       "      <td>2896.306</td>\n",
       "      <td>5682598.16</td>\n",
       "      <td>5.166981e+07</td>\n",
       "      <td>0.011589</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-26</th>\n",
       "      <td>2896.306</td>\n",
       "      <td>2893.745</td>\n",
       "      <td>3000.953</td>\n",
       "      <td>2889.014</td>\n",
       "      <td>3000.953</td>\n",
       "      <td>5763192.61</td>\n",
       "      <td>5.246691e+07</td>\n",
       "      <td>0.036131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-27</th>\n",
       "      <td>3000.953</td>\n",
       "      <td>3049.103</td>\n",
       "      <td>3087.529</td>\n",
       "      <td>3017.445</td>\n",
       "      <td>3087.529</td>\n",
       "      <td>4922871.63</td>\n",
       "      <td>4.806126e+07</td>\n",
       "      <td>0.028850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-30</th>\n",
       "      <td>3087.529</td>\n",
       "      <td>3194.722</td>\n",
       "      <td>3358.588</td>\n",
       "      <td>3153.697</td>\n",
       "      <td>3336.497</td>\n",
       "      <td>11023379.17</td>\n",
       "      <td>1.167773e+08</td>\n",
       "      <td>0.080637</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8251 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close       Volume  \\\n",
       "Day                                                                         \n",
       "1990-12-19       NaN    96.050    99.980    95.790    99.980    126000.00   \n",
       "1990-12-20    99.980   104.300   104.390    99.980   104.390     19700.00   \n",
       "1990-12-21   104.390   109.070   109.130   103.730   109.130      2800.00   \n",
       "1990-12-24   109.130   113.570   114.550   109.130   114.550      3200.00   \n",
       "1990-12-25   114.550   120.090   120.250   114.550   120.250      1500.00   \n",
       "...              ...       ...       ...       ...       ...          ...   \n",
       "2024-09-24  2748.918  2770.754  2863.152  2761.372  2863.126   4776195.45   \n",
       "2024-09-25  2863.126  2901.419  2952.451  2889.048  2896.306   5682598.16   \n",
       "2024-09-26  2896.306  2893.745  3000.953  2889.014  3000.953   5763192.61   \n",
       "2024-09-27  3000.953  3049.103  3087.529  3017.445  3087.529   4922871.63   \n",
       "2024-09-30  3087.529  3194.722  3358.588  3153.697  3336.497  11023379.17   \n",
       "\n",
       "                   Money    Return  \n",
       "Day                                 \n",
       "1990-12-19  4.940000e+05       NaN  \n",
       "1990-12-20  8.400000e+04  0.044109  \n",
       "1990-12-21  1.600000e+04  0.045407  \n",
       "1990-12-24  3.100000e+04  0.049666  \n",
       "1990-12-25  6.000000e+03  0.049760  \n",
       "...                  ...       ...  \n",
       "2024-09-24  4.427953e+07  0.041547  \n",
       "2024-09-25  5.166981e+07  0.011589  \n",
       "2024-09-26  5.246691e+07  0.036131  \n",
       "2024-09-27  4.806126e+07  0.028850  \n",
       "2024-09-30  1.167773e+08  0.080637  \n",
       "\n",
       "[8251 rows x 8 columns]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('../000001.csv')\n",
    "data['Day'] = pd.to_datetime(data['Day'],format = '%Y/%m/%d')\n",
    "data.set_index('Day',inplace = True)\n",
    "data['Close'] = pd.to_numeric(data['Close'],errors = 'coerce')\n",
    "data['Preclose'] = (data['Close']).shift(1)\n",
    "data['Return']=(data['Close']-data['Preclose'])/data['Preclose']\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Invalid frequency: QE",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "File \u001b[1;32moffsets.pyx:4447\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets._get_offset\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'QE'",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "File \u001b[1;32moffsets.pyx:4549\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets.to_offset\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32moffsets.pyx:4453\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets._get_offset\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: Invalid frequency: QE",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[20], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m data_new\u001b[38;5;241m=\u001b[39mdata[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m1995-01-01\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2024-12-31\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39mcopy()\n\u001b[1;32m----> 2\u001b[0m Month_data\u001b[38;5;241m=\u001b[39mdata_new\u001b[38;5;241m.\u001b[39mresample(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mQE\u001b[39m\u001b[38;5;124m'\u001b[39m)[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mReturn\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39mapply(\u001b[38;5;28;01mlambda\u001b[39;00m x:(\u001b[38;5;241m1\u001b[39m\u001b[38;5;241m+\u001b[39mx)\u001b[38;5;241m.\u001b[39mprod()\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m)\u001b[38;5;241m.\u001b[39mto_frame()\n\u001b[0;32m      3\u001b[0m Month_data\n",
      "File \u001b[1;32mc:\\Users\\21230\\anaconda3\\Lib\\site-packages\\pandas\\core\\generic.py:9439\u001b[0m, in \u001b[0;36mNDFrame.resample\u001b[1;34m(self, rule, axis, closed, label, convention, kind, on, level, origin, offset, group_keys)\u001b[0m\n\u001b[0;32m   9436\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m   9437\u001b[0m     axis \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m-> 9439\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m get_resampler(\n\u001b[0;32m   9440\u001b[0m     cast(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSeries | DataFrame\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28mself\u001b[39m),\n\u001b[0;32m   9441\u001b[0m     freq\u001b[38;5;241m=\u001b[39mrule,\n\u001b[0;32m   9442\u001b[0m     label\u001b[38;5;241m=\u001b[39mlabel,\n\u001b[0;32m   9443\u001b[0m     closed\u001b[38;5;241m=\u001b[39mclosed,\n\u001b[0;32m   9444\u001b[0m     axis\u001b[38;5;241m=\u001b[39maxis,\n\u001b[0;32m   9445\u001b[0m     kind\u001b[38;5;241m=\u001b[39mkind,\n\u001b[0;32m   9446\u001b[0m     convention\u001b[38;5;241m=\u001b[39mconvention,\n\u001b[0;32m   9447\u001b[0m     key\u001b[38;5;241m=\u001b[39mon,\n\u001b[0;32m   9448\u001b[0m     level\u001b[38;5;241m=\u001b[39mlevel,\n\u001b[0;32m   9449\u001b[0m     origin\u001b[38;5;241m=\u001b[39morigin,\n\u001b[0;32m   9450\u001b[0m     offset\u001b[38;5;241m=\u001b[39moffset,\n\u001b[0;32m   9451\u001b[0m     group_keys\u001b[38;5;241m=\u001b[39mgroup_keys,\n\u001b[0;32m   9452\u001b[0m )\n",
      "File \u001b[1;32mc:\\Users\\21230\\anaconda3\\Lib\\site-packages\\pandas\\core\\resample.py:1969\u001b[0m, in \u001b[0;36mget_resampler\u001b[1;34m(obj, kind, **kwds)\u001b[0m\n\u001b[0;32m   1965\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_resampler\u001b[39m(obj: Series \u001b[38;5;241m|\u001b[39m DataFrame, kind\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwds) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Resampler:\n\u001b[0;32m   1966\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m   1967\u001b[0m \u001b[38;5;124;03m    Create a TimeGrouper and return our resampler.\u001b[39;00m\n\u001b[0;32m   1968\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[1;32m-> 1969\u001b[0m     tg \u001b[38;5;241m=\u001b[39m TimeGrouper(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwds)\n\u001b[0;32m   1970\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m tg\u001b[38;5;241m.\u001b[39m_get_resampler(obj, kind\u001b[38;5;241m=\u001b[39mkind)\n",
      "File \u001b[1;32mc:\\Users\\21230\\anaconda3\\Lib\\site-packages\\pandas\\core\\resample.py:2046\u001b[0m, in \u001b[0;36mTimeGrouper.__init__\u001b[1;34m(self, freq, closed, label, how, axis, fill_method, limit, kind, convention, origin, offset, group_keys, **kwargs)\u001b[0m\n\u001b[0;32m   2043\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m convention \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstart\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mend\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124me\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124ms\u001b[39m\u001b[38;5;124m\"\u001b[39m}:\n\u001b[0;32m   2044\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUnsupported value \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mconvention\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m for `convention`\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m-> 2046\u001b[0m freq \u001b[38;5;241m=\u001b[39m to_offset(freq)\n\u001b[0;32m   2048\u001b[0m end_types \u001b[38;5;241m=\u001b[39m {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mM\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mA\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mQ\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBM\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBA\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBQ\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mW\u001b[39m\u001b[38;5;124m\"\u001b[39m}\n\u001b[0;32m   2049\u001b[0m rule \u001b[38;5;241m=\u001b[39m freq\u001b[38;5;241m.\u001b[39mrule_code\n",
      "File \u001b[1;32moffsets.pyx:4460\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets.to_offset\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32moffsets.pyx:4557\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.offsets.to_offset\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: Invalid frequency: QE"
     ]
    }
   ],
   "source": [
    "data_new=data['1995-01-01':'2024-12-31'].copy()\n",
    "Month_data=data_new.resample('ME')['Return'].apply(lambda x:(1+x).prod()-1).to_frame()\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 描述性统计"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 均值mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'Month_data' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[4], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m nd\u001b[38;5;241m.\u001b[39mmean(Month_data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2000\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2024\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mReturn\u001b[39m\u001b[38;5;124m'\u001b[39m])\n",
      "\u001b[1;31mNameError\u001b[0m: name 'Month_data' is not defined"
     ]
    }
   ],
   "source": [
    "np.mean(Month_data['2000':'2024']['Return'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'Month_data' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[5], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m Month_data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2000\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2024\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mReturn\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39mmean()\u001b[38;5;241m.\u001b[39mround(\u001b[38;5;241m4\u001b[39m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'Month_data' is not defined"
     ]
    }
   ],
   "source": [
    "Month_data['2000':'2024']['Return'].mean().round(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "round(Month_data['2000';'2024']['Return'].mean(),4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'Month_data' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[17], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m中国股票市场平均月收益率为：\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;28mround\u001b[39m(Month_data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2000\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2024\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mReturn\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39mmean()\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m100\u001b[39m,\u001b[38;5;241m4\u001b[39m),\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124m\"\u001b[39m,sep\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'Month_data' is not defined"
     ]
    }
   ],
   "source": [
    "print('中国股票市场平均月收益率为：',round(Month_data['2000':'2024']['Return'].mean()*100,4),\"%\",sep='')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid decimal literal (812739939.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[21], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    Quarter_data=data_new.resample('QE')['Return'].apply(lambda x:(1+x).prod()-1.to_frame())\u001b[0m\n\u001b[1;37m                                                                                ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid decimal literal\n"
     ]
    }
   ],
   "source": [
    "Quarter_data=data_new.resample('QE')['Return'].apply(lambda x:(1+x).prod()-1.to_frame())\n",
    "print('中国股票市场平均季度收益率为：',round(Quarter_data['2000':'2024']['Return'].mean()*100,4),\"%\",sep='')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'Month_data' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[22], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28msum\u001b[39m(Month_data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2000\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2024\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mReturn\u001b[39m\u001b[38;5;124m'\u001b[39m])\u001b[38;5;241m/\u001b[39m\u001b[38;5;28mlen\u001b[39m(Month_data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2000\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2024\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mReturn\u001b[39m\u001b[38;5;124m'\u001b[39m])\n",
      "\u001b[1;31mNameError\u001b[0m: name 'Month_data' is not defined"
     ]
    }
   ],
   "source": [
    "sum(Month_data['2000':'2024']['Return'])/len(Month_data['2000':'2024']['Return'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "\\bar{R}=\\frac{\\sum(R)}{N}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Month_data['2000':'2024']['Return'].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "均值0.005，标准差是0.07"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 分位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from statistics import quantiles\n",
    "quantiles(Month_data['2000':'2024']['Return'],n=5,method='inclusive')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'Month_data' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[23], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m Month_data[Month_data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mReturn\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m>\u001b[39m\u001b[38;5;241m0.052147828136706396\u001b[39m]\n",
      "\u001b[1;31mNameError\u001b[0m: name 'Month_data' is not defined"
     ]
    }
   ],
   "source": [
    "Month_data['2000':'2024'][Month_data['Return']>0.052147828136706396]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 方差、标准差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "\\sigma^2=\\frac{\\sum{(R-\\bar{R})^2}}{N}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Month_Variance=data_new.resample('ME')[Return].var().to_frame()\n",
    "Month_Variance.rename(columns={'Return':'Variance'},inplace=True)\n",
    "Month_variance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,ax=plt.subplots(figsize=(12,6))\n",
    "ax.plot('Return',#图片数据\n",
    "        '._',#图片类型\n",
    "        color='r'\n",
    "        label='Monthly Return'\n",
    "        linewidth=1\n",
    "        data=Month_variance)\n",
    "        ax.set_title('China stock market')\n",
    "\n",
    "        ax.set_ylabel('Variance')\n",
    "        ax.set_ylabel('Variance')\n",
    "\n",
    "#设置x轴的坐标显示\n",
    "data_format=mdates.DateFormatter('%Y')\n",
    "ax.xaxis.set_major_formatter(data_format)\n",
    "ax.xaxis.set_major_locator('data_format')\n",
    "ax.xaxis.set_major_locator(mdates.Yearlocator())\n",
    "\n",
    "#转置x轴的日期显示格式\n",
    "plt.xticks(rotation=90)\n",
    "\n",
    "#添加图例\n",
    "plt.legend(loc='upper right',fontsize=8)\n",
    "plt.show();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Month_Variance=data_new.resample('ME')[Return].apply(lambda x: sum(x**2)).to_frame()\n",
    "Month_Variance.rename(columns={'Return':'Variance'},inplace=True)\n",
    "Month_variance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,ax=plt.subplots(figsize=(12,6))\n",
    "ax.plot('Return',#图片数据\n",
    "        '._',#图片类型\n",
    "        color='r'\n",
    "        label='Monthly Return'\n",
    "        linewidth=1\n",
    "        data=Month_variance)\n",
    "        ax.set_title('China stock market')\n",
    "\n",
    "        ax.set_ylabel('Variance')\n",
    "        ax.set_ylabel('Variance')\n",
    "\n",
    "#设置x轴的坐标显示\n",
    "data_format=mdates.DateFormatter('%Y')\n",
    "ax.xaxis.set_major_formatter(data_format)\n",
    "ax.xaxis.set_major_locator('data_format')\n",
    "ax.xaxis.set_major_locator(mdates.Yearlocator())\n",
    "\n",
    "#转置x轴的日期显示格式\n",
    "plt.xticks(rotation=90)\n",
    "\n",
    "#添加图例\n",
    "plt.legend(loc='upper right',fontsize=8)\n",
    "plt.show();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 偏度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "s=\\frac{1}{N} \\sum^{N}_{i=1} [(\\frac{R-\\bar{R}}{\\sigma}^3)]\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(data_new[data_new['Return']>0])/len(data_new)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(Month_data[Month_data['Return']>0])/len(Monh_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Quarter_data=data_new.resample('QE')['Return'].apply(lambda x:(1+x).prod(-1))\n",
    "len(Quarter_data[Quarter_data]['Return']>0)/len(Quarter_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#日数据偏度\n",
    "data_new['Return'].skew()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Month_data['Return'].skew()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
